package com.vitabuybuy.tv.ui.cart.viewmodel

import androidx.lifecycle.MutableLiveData
import com.library.common.base.BaseViewModel
import com.library.common.base.MessageModel
import com.library.common.ext.rxHttpRequest
import com.library.common.net.error.msg
import com.library.common.toast.XToast
import com.vitabuybuy.tv.dto.CartGoodsDetailsDto
import com.vitabuybuy.tv.dto.CouponCenterDto
import com.vitabuybuy.tv.dto.GoodsDto
import com.vitabuybuy.tv.inter.OnGoodItemClickListener
import com.vitabuybuy.tv.network.CartRepository
import com.vitabuybuy.tv.network.ShopCenterRepository
import java.util.ArrayList

class CartViewModel : BaseViewModel() {


    val cartGoodslist = MutableLiveData<MutableList<CartGoodsDetailsDto>>()

    fun getCartGoodsDetails() {
        rxHttpRequest {
            onRequest = {
                cartGoodslist.value =  CartRepository.getCartGoodsDetails().await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    fun addCartQulitity(cartId: Int, itemPosition: Int, parentPosition: Int) {
        rxHttpRequest {
            onRequest = {
                CartRepository.addCartQuantity(cartId).await()
                defUI.msgEvent.postValue(MessageModel(1, arg1 = itemPosition, arg2 = parentPosition))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun reduceCartQulitity(cartId: Int, itemPosition: Int, parentPosition: Int) {
        rxHttpRequest {
            onRequest = {
                CartRepository.reduceCartQuantity(cartId).await()
                defUI.msgEvent.postValue(MessageModel(2, arg1 = itemPosition, arg2 = parentPosition))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }

    fun deleteCart(idList: ArrayList<Int>) {
        rxHttpRequest {
            onRequest = {
                CartRepository.deleteCart(idList).await()
                defUI.msgEvent.postValue(MessageModel(3))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
    val goodlist = MutableLiveData<List<GoodsDto>>()

    fun getGoodsList(id: Int, sort: String, pageNumber: Int) {
        rxHttpRequest {
            onRequest = {
                goodlist.value = ShopCenterRepository.getHomeGoodList(id, sort, pageNumber, 10).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }

    val couponsList = MutableLiveData<MutableList<CouponCenterDto>>()

    fun getStoreCouponsList(storeId: Int) {
        rxHttpRequest {
            onRequest = {
                couponsList.value = CartRepository.getCouponsList(storeId).await()
            }
            onError = {
                XToast.normal(it.msg)
            }
        }
    }


    fun saveACoupon(couponId:Int, position: Int) {
        rxHttpRequest {
            onRequest = {
                CartRepository.saveACoupon(couponId).await()
                defUI.msgEvent.postValue(MessageModel(4,arg1 = position))
            }
            onError = {
                XToast.normal(it.msg)
            }
            isShowDialogRequest = true
        }
    }
}
